using UnityEngine;
using UnityEditor;

public static class CustomEditorGUI
{
    /// <summary>
    /// 绘制带有亮色背景、顶部装饰条的居中标题，支持高度、字体大小和文字偏移调节
    /// </summary>
    /// <param name="labelName">标题文本</param>
    /// <param name="tooltip">提示文本</param>
    /// <param name="backgroundColor">背景颜色，默认使用浅灰色</param>
    /// <param name="decoratorColor">顶部装饰条颜色，默认使用白色</param>
    /// <param name="height">标题行高度，默认25像素</param>
    /// <param name="fontSize">字体大小，默认12</param>
    /// <param name="verticalOffset">文字上下偏移量，默认0</param>
    public static void HighlightedHeader(string labelName, string tooltip = "", 
                                        Color? backgroundColor = null, Color? decoratorColor = null,
                                        float height = 25f, int fontSize = 40, float verticalOffset = 0f)
    {
        EditorGUILayout.Space();
        
        // 设置默认颜色
        Color bgColor = backgroundColor ?? new Color(0.0f, 0.0f, 0.0f, 0.25f);
        Color decorColor = decoratorColor ?? new Color(0.0f, 0.0f, 0.0f, 0.5f);
        
        // 保存当前GUIStyle
        GUIStyle originalStyle = EditorStyles.label;

        // 创建居中对齐的样式并设置字体大小
        GUIStyle centeredStyle = new GUIStyle(originalStyle)
        {
            alignment = TextAnchor.MiddleCenter,
            fontSize = fontSize
        };

        // 计算所需的矩形区域，设置自定义高度
        Rect rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(height));
        {
            // 手动绘制背景
            EditorGUI.DrawRect(rect, bgColor);
            
            // 绘制顶部细白条装饰 (高度为2像素)
            Rect decorRect = new Rect(rect.x, rect.y, rect.width, 2);
            EditorGUI.DrawRect(decorRect, decorColor);

            // 绘制标题文本，应用垂直偏移
            GUIContent content = new GUIContent(labelName, tooltip);
            
            // 如果有垂直偏移，需要手动计算绘制位置
            if (Mathf.Abs(verticalOffset) > 0.001f)
            {
                // 保存当前矩阵用于偏移
                Matrix4x4 originalMatrix = GUI.matrix;
                // 应用垂直偏移
                GUI.matrix = Matrix4x4.TRS(new Vector3(0, verticalOffset, 0), Quaternion.identity, Vector3.one);
                GUILayout.Label(content, centeredStyle);
                // 恢复原始矩阵
                GUI.matrix = originalMatrix;
            }
            else
            {
                // 无偏移时直接绘制
                GUILayout.Label(content, centeredStyle);
            }
        }
        EditorGUILayout.EndHorizontal();
        
        EditorGUILayout.Space();
    }

    // 重载方法，接收GUIContent参数
    public static void HighlightedHeader(GUIContent content, Color? backgroundColor = null, 
                                        Color? decoratorColor = null, float height = 25f, 
                                        int fontSize = 13, float verticalOffset = 2f)
    {
        HighlightedHeader(content.text, content.tooltip, backgroundColor, decoratorColor, 
                         height, fontSize, verticalOffset);
    }
}
    